/***************************************************************************
 *   Copyright (C) 2007 by Ivan Vasić                    *
 *   ivasic@gmail.com                     *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifndef TRACKERSERVER_H
#define TRACKERSERVER_H

#include <QTcpServer>

/**
 * @class TrackerServer
 * @brief HTTP server klasa.
 * @author Ivan Vasić <ivasic@gmail.com>
 *
 * Objekat ove klase je zadužen da prima konekcije klijenata i kreira nove klijentske niti (ClientThread).
*/
class TrackerServer : public QTcpServer
{
		Q_OBJECT

	public:
		///Konstruktor
		TrackerServer(QObject* parent = 0);

		///Destruktor
		~TrackerServer();

		///Proverava da li je server startovan i prima konekcije
		bool isRunning() const { return m_running; }

		void setMax_connections(int val) { m_max_connections = val; }
		int max_connections() const { return m_max_connections; }
				
		int num_connections() const { return m_num_connections; }
		int num_connections_denied() const { return m_num_connections_denied; }
		
	signals:
		void numConnectionsChanged(int numConnections);

	public slots:

		///Starts this server
		void start();

		///Stops this server
		void stop();

		///Obavestava server da je jedna od konekcija ugasena. U sustini samo updateuje m_num_connections
		void connectionClosed();

	protected:
		/**
		 * @brief Handler za novu konekciju.
		 * 
		 * Ova funkcija je nasleđena od klase QTcpServer i poziva se kada naš server primi konekciju od novog klijenta.
		 * Parametar socketDescriptor se prosledjuje klijentskoj niti koja kreira novi Socket za ovaj deskriptor.
		 * 
		 * @see ClientThread::ClientThread()
		 */
		void incomingConnection(int socketDescriptor);

	private:

		///Da li prima konekcije ili ne (startovan ili ne)
		bool m_running;

		/**
		 * Vreme kada je server startovan.
		 * 0 ako server nije startovan tj. ako je m_running = FALSE
		 */
		quint32 m_timeStarted;

		///Maksimalni broj dozvoljenih konekcija
		int m_max_connections;

		///Trenutni broj konekcija
		int m_num_connections;

		///Ukupan broj odbijenih konekcija (ukoliko je aktivan limit)
		int m_num_connections_denied;
};

#endif
